package com.example.ucs_alg_node_manager.utils;

import java.net.MalformedURLException;
import java.net.URL;

public class UcsPatterns {

    /**
     * 基地址
     */
    public static final String BASE_URL = "http://62.234.16.239:9997/api/v1/algnode";

    public static final String STAT_URL = BASE_URL + "/stat";

    public static final String ABOUT_URL = BASE_URL + "/about";

    public static final String SUBMIT_URL = BASE_URL + "/task/submit";

    public static final String RELOAD_URL = BASE_URL + "/reload";

    public static final String SKIP_URL = BASE_URL + "/task/skip";

    public static final String GET_MODELS_URL = BASE_URL + "/model/all";

    public static final String UPLOAD_MODEL_URL = BASE_URL + "/model/upload";


    private static final String URL_PATTERN =
            "^(https?|ftp)://[^\\s/$.?#].[^\\s]*$";


    // 解析地址
    public static String hostUrl(String addr){
        int start = addr.indexOf("://") + 3; // 找到"://"之后的位置
        int end = addr.indexOf("/", start); // 找到"/"的位置
        if (end == -1) {
            end = addr.length(); // 如果没有"/"，则取字符串长度
        }
        return addr.substring(start, end); // 截取地址和端口部分
    }


    // 判断地址格式是否合法
    public static boolean isValidUrl(String urlString) {
        if (urlString == null) {
            return false;
        }
        if (!urlString.matches(URL_PATTERN)) {
            return false;
        }
        try {
            new URL(urlString);
            return true;
        } catch (MalformedURLException e) {
            return false;
        }
    }


    public static String getBaseURL(String url) {
        try {
            URL fullURL = new URL(url);
            String baseURL = fullURL.getProtocol() + "://" + fullURL.getHost() + (fullURL.getPort() != -1 ? ":" + fullURL.getPort() : "");
            return baseURL;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


}
